<?php
/**
 * 获取已上传的文件列表
 * User: Jinqn
 * Date: 14-04-09
 * Time: 上午10:17
 */
include "Qiniu_List.php";

/* 判断类型 */
switch ($_GET['action']) {
    /* 列出文件 */
    case 'listfile':
        $allowFiles = $CONFIG['fileManagerAllowFiles'];
        $listSize = $CONFIG['fileManagerListSize'];
        break;
    /* 列出图片 */
    case 'listimage':
    default:
        $allowFiles = $CONFIG['imageManagerAllowFiles'];
        $listSize = $CONFIG['imageManagerListSize'];

}
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
/* 获取参数 */
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;

$web_config = getWebConfig();
if ($web_config['web_id'] == 0 || $web_config['module'] == 'other') {
    return json_encode(array(
        "state" => "param not found",
    ));
}
/* 获取文件列表 */
$Qiniu_List = Qiniu_List::getInstance();
$Qiniu_List -> getUrl("{$web_config['prefix']}_",$_GET['marker']??'',$size);
$files = $Qiniu_List -> listFiles();

if (empty($files['items'])) {
    return json_encode(array(
        "state" => "no match file",
    ));
}
foreach ($files['items'] as $key => $value) {
    $lists[] = [
        'url'=> $HOST.'/'.$value['key'],
        'mtime'=> $value['putTime']
    ];
}
/* 返回数据 */
$result = json_encode(array(
    "state" => "SUCCESS",
    "list" => $lists,
    "marker" => $files['marker']??"",
    "start" => $start,
    "total" => count($lists)
));

return $result;
